#include "Brick.h"
#include "Bonus.h"
#include "Expand.h"
#include "Stick.h"
#include "Clone.h"
#include "DrawList.h"
#include "Renderer.h"
#include "Bass.h"

GLuint CBrick::m_uiTexture = 0;
GLuint CBrick::m_uiVBOTexCoords = 0;
GLuint CBrick::m_uiVBOVertices = 0;
GLuint CBrick::m_uiVBOIndices = 0;
CModel* CBrick::m_pModel = NULL;


CBrick::CBrick()
{
	//non-ODE init routnes
	m_iRemoveTimer = -1;
	m_fSize = 1.0f;

	//material:
	m_matMaterial.d3Diffuse = double3(1.0, 1.0, 1.0);
	m_matMaterial.d3Specular = double3(0.0, 0.0, 0.0);
	m_matMaterial.dAlpha = 0.0;
	m_matMaterial.dShiness = 00.0;

	//ODE init routines
	m_body = dBodyCreate( m_pPhysics->GetWorld() );
		

	m_geom = dCreateBox(m_pPhysics->GetSpace(), m_fSize, m_fSize, m_fSize);
		
	dGeomSetBody(m_geom, m_body);

	dMassSetBox(&m_mass, 1, m_fSize, m_fSize, m_fSize);
	dBodySetMass(m_body, &m_mass);

	dBodySetPosition(m_body, 0.0f, 0.0f, 0.0f);

	dBodySetData(m_body, this);

	m_iValue = 25;
		
//	dRSetIdentity (R);
//	dBodySetRotation(body, R);
}

CBrick::~CBrick(void)
{
	dBodyDestroy(m_body);
	dGeomDestroy(m_geom);
}

int CBrick::Draw()
{
	glPushMatrix();

	SetupMatrices();

	m_pRenderer->SetMaterial(m_matMaterial);
	m_pModel->DrawModel();


	glPopMatrix();
	return 0;
}

int CBrick::Update()
{
	if(m_iRemoveTimer==0)
	{
		m_pBat->Score(m_iValue);
		//random bonus should be thrown
		if(rand()%5 == 0)
		{
			CBonus* pBonus;

			//randomize bonus type
			int iRand = rand()%(BT_CLONE);
			if(iRand==0)
			{
				pBonus = new CExpand();
			}
			if(iRand==1)
			{
				pBonus = new CClone();
			}
			if(iRand==2)
			{
				pBonus = new CStick();
			}
			//pBonus = new CClone();
			
			pBonus->SetBatDest(m_pBat);

			const double* dPos = dBodyGetPosition(m_body);

			pBonus->SetPosition(dPos[0],  dPos[1], dPos[2]);
			m_pDrawList->AddObject(pBonus);
		}
		m_pSoundManager->PlaySoundA("data\\sounds\\dropping-2.wav",false);
		Remove();
		m_pDrawList->EnableAll();
	}
	else
	{
		m_iRemoveTimer--;
	}
	return 0;
}

int CBrick::SetPosition(double dX, double dY, double dZ)
{
	dBodySetPosition(m_body, dX, dY, dZ);
	return 0;
}

int CBrick::Hit(CBall* pBall)
{
	m_iRemoveTimer = 5;
	dBodyEnable(m_body);
	m_pBat = pBall->GetRecent();
	return 0;
}

void CBrick::LoadTexture()
{
	m_uiTexture = CUtils::LoadRAW("data\\images\\brick.raw", 256, 256);
}

void CBrick::LoadModel(float fSize)
{
	//create data buffers:

	CRenderer::SVert vertBuf[4*6] = //6 sides, 4 vertices per side
	{
		//back side
		CRenderer::SVert( float3( -fSize/2, -fSize/2, -fSize/2), float3(  0.0f,  0.0f, -1.0f), float2( 0.0f, 0.0f)),
		CRenderer::SVert( float3( -fSize/2,  fSize/2, -fSize/2), float3(  0.0f,  0.0f, -1.0f), float2( 0.0f, 1.0f)),
		CRenderer::SVert( float3(  fSize/2,  fSize/2, -fSize/2), float3(  0.0f,  0.0f, -1.0f), float2( 1.0f, 1.0f)),
		CRenderer::SVert( float3(  fSize/2, -fSize/2, -fSize/2), float3(  0.0f,  0.0f, -1.0f), float2( 1.0f, 0.0f)),

		//front side
		CRenderer::SVert( float3( -fSize/2, -fSize/2,  fSize/2), float3(  0.0f,  0.0f,  1.0f), float2( 0.0f, 0.0f)),
		CRenderer::SVert( float3( -fSize/2,  fSize/2,  fSize/2), float3(  0.0f,  0.0f,  1.0f), float2( 0.0f, 1.0f)),
		CRenderer::SVert( float3(  fSize/2,  fSize/2,  fSize/2), float3(  0.0f,  0.0f,  1.0f), float2( 1.0f, 1.0f)),
		CRenderer::SVert( float3(  fSize/2, -fSize/2,  fSize/2), float3(  0.0f,  0.0f,  1.0f), float2( 1.0f, 0.0f)),

		//bottom side
		CRenderer::SVert( float3( -fSize/2, -fSize/2, -fSize/2), float3(  0.0f, -1.0f,  0.0f), float2( 0.0f, 0.0f)),
		CRenderer::SVert( float3( -fSize/2, -fSize/2,  fSize/2), float3(  0.0f, -1.0f,  0.0f), float2( 0.0f, 1.0f)),
		CRenderer::SVert( float3(  fSize/2, -fSize/2,  fSize/2), float3(  0.0f, -1.0f,  0.0f), float2( 1.0f, 1.0f)),
		CRenderer::SVert( float3(  fSize/2, -fSize/2, -fSize/2), float3(  0.0f, -1.0f,  0.0f), float2( 1.0f, 0.0f)),

		//top side
		CRenderer::SVert( float3( -fSize/2,  fSize/2, -fSize/2), float3(  0.0f,  1.0f,  0.0f), float2( 0.0f, 0.0f)),
		CRenderer::SVert( float3( -fSize/2,  fSize/2,  fSize/2), float3(  0.0f,  1.0f,  0.0f), float2( 0.0f, 1.0f)),
		CRenderer::SVert( float3(  fSize/2,  fSize/2,  fSize/2), float3(  0.0f,  1.0f,  0.0f), float2( 1.0f, 1.0f)),
		CRenderer::SVert( float3(  fSize/2,  fSize/2, -fSize/2), float3(  0.0f,  1.0f,  0.0f), float2( 1.0f, 0.0f)),

		//left side
		CRenderer::SVert( float3( -fSize/2, -fSize/2, -fSize/2), float3( -1.0f,  0.0f,  0.0f), float2( 0.0f, 0.0f)),
		CRenderer::SVert( float3( -fSize/2, -fSize/2,  fSize/2), float3( -1.0f,  0.0f,  0.0f), float2( 0.0f, 1.0f)),
		CRenderer::SVert( float3( -fSize/2,  fSize/2,  fSize/2), float3( -1.0f,  0.0f,  0.0f), float2( 1.0f, 1.0f)),
		CRenderer::SVert( float3( -fSize/2,  fSize/2, -fSize/2), float3( -1.0f,  0.0f,  0.0f), float2( 1.0f, 0.0f)),

		//right side
		CRenderer::SVert( float3(  fSize/2, -fSize/2, -fSize/2), float3(  1.0f,  0.0f,  0.0f), float2( 0.0f, 0.0f)),
		CRenderer::SVert( float3(  fSize/2, -fSize/2,  fSize/2), float3(  1.0f,  0.0f,  0.0f), float2( 0.0f, 1.0f)),
		CRenderer::SVert( float3(  fSize/2,  fSize/2,  fSize/2), float3(  1.0f,  0.0f,  0.0f), float2( 1.0f, 1.0f)),
		CRenderer::SVert( float3(  fSize/2,  fSize/2, -fSize/2), float3(  1.0f,  0.0f,  0.0f), float2( 1.0f, 0.0f))
	};

	unsigned int iIndices[6*6] = {
		//back side
		 0, 1, 2, 0, 2, 3,
		//front side
		 4, 5, 6, 4, 6, 7,
		//bottom side
		 8, 9,10, 8,10,11,
		//top side
		12,13,14,12,14,15,
		//left side
		16,17,18,16,18,19,
		//right side
		20,21,22,20,22,23
	};	

	//create and bind VBO
	glGenBuffersARB(1, &m_uiVBOVertices);
	glBindBufferARB(GL_ARRAY_BUFFER_ARB, m_uiVBOVertices);
	glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(CRenderer::SVert)*4*6, vertBuf, GL_STATIC_DRAW_ARB);

	//create and bind IBO
	glGenBuffersARB(1, &m_uiVBOIndices);
	glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, m_uiVBOIndices);
	glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(int)*6*6, iIndices, GL_STATIC_DRAW_ARB);
}